package com.bysj.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bysj.entity.Adv;
import com.bysj.fenye.pagevo.AdvPageVO;
import com.bysj.service.AdvService;
import com.bysj.service.impl.AdvServiceImpl;

public class AdvServlet extends HttpServlet {
	private AdvPageVO pagevo=new AdvPageVO(10);
	private AdvService as=new AdvServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m=request.getParameter("m");
		String method=request.getParameter("method");
		if("pre".equals(method)){
			this.pagevo.setPrevious();
		}else if("next".equals(method)){
			this.pagevo.setNext();
		}else if("last".equals(method)){
			this.pagevo.setLast();
		}else if("index".equals(method)){
			String ind=request.getParameter("ind");
			this.pagevo.setIndex(Integer.parseInt(ind.trim()));
		}else{
			this.pagevo.setFirst();
		}
		if("AllAdv".equals(m)){
			request.getSession().setAttribute("alist", pagevo);
			request.getRequestDispatcher("../adv/alladv.jsp").forward(request, response);
		}else if("detailAdv".equals(m)){
			Adv adv=new Adv();
			adv.setId(Integer.parseInt(request.getParameter("id")));
			Adv a=as.queryAdv(adv);
			request.getSession().setAttribute("adv", a);
			request.getRequestDispatcher("../adv/detailadv.jsp").forward(request, response);
		}else if("AddNewAdv".equals(m)){
			String title=request.getParameter("title");
			String time=request.getParameter("time");
			String type=request.getParameter("type");
			String content=request.getParameter("advFCKeditor");
			Adv adv=new Adv();
			adv.setTitle(title);
			adv.setTime(time);
			adv.setType(type);
			adv.setContent(content);
			as.addAdv(adv);
			request.getSession().setAttribute("alist", pagevo);
			request.getRequestDispatcher("../adv/alladv.jsp").forward(request, response);
		}else if("DelAdv".equals(m)){
			Adv adv=new Adv();
			adv.setId(Integer.parseInt(request.getParameter("id")));
			as.delAdv(adv);
			request.getSession().setAttribute("alist", pagevo);
			request.getRequestDispatcher("../adv/alladv.jsp").forward(request, response);
		}else if("updateAdvDocById".equals(m)){
			request.getSession().setAttribute("updateAdvDocById", request.getParameter("id"));
			request.getRequestDispatcher("../adv/updateAdvDoc.jsp").forward(request, response);
			
		}
	}

}